package net.wantongtech.xa.uploadbasicinfo.service;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.analysis.ExcelReadExecutor;
import com.alibaba.excel.read.metadata.ReadSheet;
import lombok.extern.slf4j.Slf4j;
import net.wantongtech.xa.uploadbasicinfo.dao.CancelCardRefundDao;
import net.wantongtech.xa.uploadbasicinfo.listener.RefundModelListener;
import net.wantongtech.xa.uploadbasicinfo.vo.RefundExcelModel;
import org.apache.commons.lang.ArrayUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.Arrays;
import java.util.List;

/**
 * 解析销卡退费Excel
 *
 * @author MaShibo
 * 日期 :  2020/1/3
 */
@Slf4j
@Service
public class ParseRefundExcelService {

    @Resource
    private CancelCardRefundDao cancelCardRefundDao;


    public void parse() {
        File[] files = new File[]{new File("C:\\Users\\Administrator\\Desktop\\20200615退费数据\\20200615-refund.xlsx")};
        if (ArrayUtils.isEmpty(files)) {
            return;
        }
        Arrays.stream(files).forEach(file -> {
            String fileName = file.getName();
            log.info("开始解析Excel文件：{}", fileName);
            ExcelReader excelReader = EasyExcel.read(file,
                    RefundExcelModel.class, new RefundModelListener(cancelCardRefundDao, fileName))
                    .ignoreEmptyRow(true)
                    .autoTrim(true)
                    .build();
            ExcelReadExecutor executor = excelReader.excelExecutor();
            List<ReadSheet> sheetList = executor.sheetList();
            sheetList.forEach(excelReader::read);
            excelReader.finish();
        });
    }

    public static void main(String[] args) {
        new ParseRefundExcelService().parse();
    }

}
